<script setup lang="ts">
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CustomNavbar from './components/CustomNavbar.vue'
import XtxSwiper from '@/components/XtxSwiper.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { getHomeBannerAPI } from '@/services/home'
import { getHomeCategoryAPI } from '@/services/home'
import { getHotAPI } from '@/services/home'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { XtxGuessInstance } from '@/types/components'
import { useGuessList } from '@/composables'

const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
const getHotData = async () => {
  const res = await getHotAPI()
  hotList.value = res.result
}
// 页面加载
const isLoadding = ref(false)
onLoad(async () => {
  isLoadding.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotData()]) // 关闭动画
  isLoadding.value = false
})
// 获取猜你喜欢组件实例
// const guessRef = ref<XtxGuessInstance>()
// const onScrolltolower = () => {
//   guessRef.value?.getMore()
// }
const { guessRef, onScrolltolower } = useGuessList()
const isTriggered = ref(false)
const onResherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotData()]) // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <CustomNavbar />
  <scroll-view
    scroll-y="true"
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    @refresherrefresh="onResherrefresh"
    refresher-enabled
    :refresher-triggered="isTriggered"
  >
    <PageSkeleton v-if="isLoadding" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <HotPanel :list="hotList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
/* #ifdef APP-PLUS */
#app,
/* #endif */
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
